<?php
	
class Table_Mariages extends Zend_Db_Table_Abstract
{
    protected $_name    = 'mariage';
    protected $_primary = array('VIP_NUMERO','VIP_VIP_NUMERO','DATE_EVENEMENT');
    protected $_sequence = true; //la table a un autoincrément
 	
    
	
	public function getMByNum($numVip){
		$req1= $this->select()
									->setIntegrityCheck(false)
									->from(array('m'=>'mariage'),
									array('MARIAGE_LIEU', 'DATE_EVENEMENT', 'MARIAGE_FIN'))
									->join(array('v1'=>'vip'),
									'v1.VIP_NUMERO = m.VIP_NUMERO',
									array(''))
									->join(array('v2'=>'vip'),
									'm.VIP_VIP_NUMERO = v2.VIP_NUMERO',
									array('v2.VIP_NOM','v2.VIP_NUMERO','v2.VIP_TEXTE','v2.VIP_PRENOM'))
									->where('v1.VIP_NUMERO = ?', $numVip)
									->distinct();

		$req2= $this->select()
									->setIntegrityCheck(false)
									->from(array('m'=>'mariage'),
									array('MARIAGE_LIEU', 'DATE_EVENEMENT', 'MARIAGE_FIN'))
									->join(array('v1'=>'vip'),
									'v1.VIP_NUMERO = m.VIP_VIP_NUMERO',
									array(''))
									->join(array('v2'=>'vip'),
									'm.VIP_NUMERO = v2.VIP_NUMERO',
									array('v2.VIP_NOM','v2.VIP_NUMERO','v2.VIP_TEXTE','v2.VIP_PRENOM'))
									->where('v1.VIP_NUMERO = ?', $numVip)
									->distinct();
							
		$req= $this->select()
    								->union(array($req1, $req2));
    
		$resu=$this->fetchAll($req);
		return $resu;
	}
	
	public function getMaByNum($numVip){
		$req1= $this->select()
									->setIntegrityCheck(false)
									->from(array('m'=>'mariage'),
									array('MARIAGE_LIEU', 'DATE_EVENEMENT', 'MARIAGE_FIN', 'MARIAGE_MOTIFFIN'))
									->join(array('v1'=>'vip'),
									'v1.VIP_NUMERO = m.VIP_NUMERO',
									array(''))
									->join(array('v2'=>'vip'),
									'm.VIP_VIP_NUMERO = v2.VIP_NUMERO',
									array('v2.VIP_NOM','v2.VIP_NUMERO','v2.VIP_TEXTE','v2.VIP_PRENOM'))
									->where('v1.VIP_NUMERO = ?', $numVip)
									->distinct();

		$req2= $this->select()
									->setIntegrityCheck(false)
									->from(array('m'=>'mariage'),
									array('MARIAGE_LIEU', 'DATE_EVENEMENT', 'MARIAGE_FIN', 'MARIAGE_MOTIFFIN'))
									->join(array('v1'=>'vip'),
									'v1.VIP_NUMERO = m.VIP_VIP_NUMERO',
									array(''))
									->join(array('v2'=>'vip'),
									'm.VIP_NUMERO = v2.VIP_NUMERO',
									array('v2.VIP_NOM','v2.VIP_NUMERO','v2.VIP_TEXTE','v2.VIP_PRENOM'))
									->where('v1.VIP_NUMERO = ?', $numVip)
									->distinct();
							
		$req= $this->select()
    								->union(array($req1, $req2));
    
		$resu=$this->fetchRow($req);
		return $resu;
	}
	
	public function addMariage($numVip, $numsecondvip, $lieu, $datedebut, $datefin, $motiffin){
		$tabledebut = explode('/', $datedebut);
		$dated = $tabledebut[2].'-'.$tabledebut[1].'-'.$tabledebut[0];
		$tablefin = explode('/', $datefin);
		$datef = ($datefin==null)?null:$tablefin[2].'-'.$tablefin[1].'-'.$tablefin[0];
		
		$data = array('VIP_VIP_NUMERO'=> $numsecondvip,
					  'VIP_NUMERO'=> $numVip,
					  'DATE_EVENEMENT'=> $dated,
					  'MARIAGE_LIEU'=> $lieu,
					  'MARIAGE_FIN'=> $datef,
					  'MARIAGE_MOTIFFIN' => $motiffin);
		$this->insert($data);
	}
	
	public function deleteVip($numvip){
		$this->delete('VIP_NUMERO = '.$numvip.' OR VIP_VIP_NUMERO = '.$numvip);
	}
}